﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Data;
using System.Diagnostics;
using DevExpress.XtraEditors.Repository;
using DevExpress.XtraExport;
using DevExpress.XtraGrid.Views;
using DevExpress.XtraEditors;
using DevExpress.Utils;
using System.Data.OleDb;
using System.Windows.Forms;
using DevExpress.XtraGrid.Export;
using RTSoft.Entity;
namespace RTSoft
{
    public class Library
    {
        private string _strField;

        private object _value;
        public string FieldName
        {
            get { return _strField; }
            set { _strField = value; }
        }

        public object FieldValue
        {
            get { return _value; }
            set { _value = value; }
        }

        public static DataTable ImportFromExcel(string filePath, string sheetName)
        {
            DataTable dt = new DataTable();
            filePath = ShowOpenFileDialog();
            if (!string.IsNullOrEmpty(filePath))
            {
                string sConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0; Data Source=" + filePath + "; Extended Properties=Excel 8.0;";

                OleDbConnection objConn = new OleDbConnection(sConnectionString);

                objConn.Open();

                OleDbCommand objCmdSelect = new OleDbCommand("SELECT * FROM [" + sheetName + "$]", objConn);

                OleDbDataAdapter objAdapter1 = new OleDbDataAdapter();

                objAdapter1.SelectCommand = objCmdSelect;

                objAdapter1.Fill(dt);

                objConn.Close();
            }
            return dt;
        }

        public static void ExportToExcel(DevExpress.XtraEditors.XtraForm frm, string strName, DevExpress.XtraGrid.Views.Grid.GridView view)
        {
            string fileName = ShowSaveFileDialog("Microsoft Excel Document", "Microsoft Excel|*.xls", strName);
            if (!string.IsNullOrEmpty(fileName))
            {
                ExportTo(frm, new ExportXlsProvider(fileName), view);
                OpenFile(fileName);
            }
        }

        private static string ShowSaveFileDialog(string title, string filter, string fileName)
        {
            SaveFileDialog dlg = new SaveFileDialog();
            string name = fileName;
            int n = name.LastIndexOf(".") + 1;
            if (n > 0)
            {
                name = name.Substring(n, name.Length - n);
            }
            dlg.Title = "Export To " + title;
            dlg.FileName = name;
            dlg.Filter = filter;
            if (dlg.ShowDialog() == System.Windows.Forms.DialogResult.OK)
            {
                return dlg.FileName;
            }
            return "";
        }

        private static string ShowOpenFileDialog()
        {
            OpenFileDialog dlg = new OpenFileDialog();
            dlg.Filter = "Excel 97-2003 (*.xls)|*.xls|Excel 2007 (*.xlsx)|*.xlsx";
            if (dlg.ShowDialog() == System.Windows.Forms.DialogResult.OK)
            {
                return dlg.FileName;
            }
            return "";
        }

        private static void ExportTo(DevExpress.XtraEditors.XtraForm frm, IExportProvider provider, DevExpress.XtraGrid.Views.Grid.GridView view)
        {
            Cursor currentCursor = System.Windows.Forms.Cursor.Current;
            System.Windows.Forms.Cursor.Current = Cursors.WaitCursor;
            frm.FindForm().Refresh();
            BaseExportLink link = view.CreateExportLink(provider);
            (link as GridViewExportLink).ExpandAll = false;
            //AddHandler link.Progress, AddressOf Export_Progress
            link.ExportTo(true);
            provider.Dispose();
            //RemoveHandler link.Progress, AddressOf Export_Progress
            System.Windows.Forms.Cursor.Current = currentCursor;
        }

        private static void OpenFile(string fileName)
        {
            if (ShowQuestionMessage("Mở File Này?") == true)
            {
                try
                {
                    System.Diagnostics.Process process = new System.Diagnostics.Process();
                    process.StartInfo.FileName = fileName;
                    process.StartInfo.Verb = "Open";
                    process.StartInfo.WindowStyle = System.Diagnostics.ProcessWindowStyle.Normal;
                    process.Start();
                }
                catch (Exception ex)
                {
                    Library.ShowMessage(ex.ToString());
                }
            }
        }

        public static void ShowMessage(string strMessage)
        {
            frmShowMessage frm = new frmShowMessage();
            frm.lblMessage.Text = strMessage;            
            frm.ShowDialog();
        }

        public static bool ShowQuestionMessage(string strMessage)
        {
            bool result = false;
            frmShowQuestionMessage frm = new frmShowQuestionMessage();
            frm.lblMessage.Text = strMessage;
            frm.ShowDialog();
            //result = frm           
           // result = frmShowQuestionMessage.status;
            result = frm.Status;
            return result;
        }
    }
    public class SystemInfo
    {
        private string _serverName;
        private string _dataBase;
        private int _timeOut;
        private string _userName;
        private string _passWord;
        private string _eMail;
        private string _user;
        public string ServerName
        {
            get { return _serverName; }
            set { _serverName = value; }
        }
        public string Database
        {
            get { return _dataBase; }
            set { _dataBase = value; }
        }
        public int TimeOut
        {
            get { return _timeOut; }
            set { _timeOut = value; }
        }
        public string UserName
        {
            get { return _userName; }
            set { _userName = value; }
        }
        public string PassWord
        {
            get { return _passWord; }
            set { _passWord = value; }
        }
        public string User
        {
            get { return _user; }
            set { _user = value; }
        }
        public string Email
        {
            get { return _eMail; }
            set { _eMail = value; }
        }

    }
}